package com.xl.generator.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xl.generator.common.ActionResult;
import com.xl.generator.common.ActionResultService;
import com.xl.generator.domain.CommonUser;
import com.xl.generator.domain.CompetitionDetail;
import com.xl.generator.pojo.PageList;
import com.xl.generator.service.CommonUserService;
import com.xl.generator.service.CompetitionDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈〉<br>
 *
 * @author lvln5
 * @Description
 * @date 2022/11/12 19:39
 */
@Api(value = "比赛的增删改查")
@RestController
@RequestMapping("competitionDetail")
public class CompetitionDetailController {
	@Resource
	private CompetitionDetailService competitionDetailService;

	@Autowired
	private HttpServletRequest httpServletRequest;

	@Resource
	private CommonUserService commonUserService;

	@ApiOperation(value = "分页查询所有竞赛", notes="登录成功状态码为o,否侧为-1用户不存在")
	@PostMapping ("list")
	public ActionResult test(@RequestParam("current") Integer current,@RequestParam("Size") Integer size){
		IPage<CompetitionDetail> commonUserIPage = new Page<>();
		commonUserIPage.setCurrent(current);
		commonUserIPage.setSize(size);
		IPage<CompetitionDetail> page = competitionDetailService.page(commonUserIPage);
		PageList pageList = new PageList();
		pageList.setData(page);
		pageList.setTotal(commonUserIPage.getTotal());
		return ActionResultService.callBackResultOK(pageList);
	}

	@ApiOperation(value = "分页查询所有竞赛", notes="登录成功状态码为o,否侧为-1用户不存在")
	@PostMapping ("listDegree")
	public ActionResult test123(@RequestParam("current") Integer current,@RequestParam("Size") Integer size){
		String userId = httpServletRequest.getHeader("userId");
		CommonUser commonUser = new CommonUser();
		if (userId!=null){
			commonUser = commonUserService.getById(Integer.parseInt(userId));
		}
		QueryWrapper<CompetitionDetail> qw = new QueryWrapper<>();
		qw.lambda().le(CompetitionDetail::getId,commonUser.getDegree());
		IPage<CompetitionDetail> commonUserIPage = new Page<>();
		commonUserIPage.setCurrent(current);
		commonUserIPage.setSize(size);
		IPage<CompetitionDetail> page = competitionDetailService.page(commonUserIPage,qw);
		PageList pageList = new PageList();
		pageList.setData(page);
		pageList.setTotal(commonUserIPage.getTotal());
		return ActionResultService.callBackResultOK(pageList);
	}


	/**
	 * @param competitionDetail
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "添加竞赛", notes="注册成功状态码为o,否侧为-1为用户存在")
	@PostMapping("add")
	public ActionResult register(@RequestBody CompetitionDetail competitionDetail){
		//否则不在插入
		boolean save = competitionDetailService.save(competitionDetail);
		if (save){
			return ActionResultService.callBackResultOK("添加竞赛成功");
		}
		return ActionResultService.callBackResultOK("添加竞赛失败");
	}

	/**
	 * @param competitionDetail
	 * @return {@code ActionResult }
	 */
	@ApiOperation(value = "删除竞赛", notes="删除成功状态码为o,否侧为-1失败")
	@PostMapping("del")
	public ActionResult del(@RequestBody CompetitionDetail competitionDetail){

//		LambdaQueryWrapper<CompetitionDetail> queryWrapper = new LambdaQueryWrapper<>();
//		queryWrapper.eq(CompetitionDetail::getId,competitionDetail.getId());
		boolean remove = competitionDetailService.removeById(competitionDetail);
		if (remove){
			return	ActionResultService.callBackResultOK("删除成功");
		}
		return ActionResultService.callBackResultFail("删除失败");
	}
	@ApiOperation(value = "更新竞赛", notes="更新成功状态码为o,否侧为-1失败")
	@PostMapping("update")
	public ActionResult updata(@RequestBody  CompetitionDetail competitionDetail){
//		LambdaQueryWrapper<CompetitionDetail> queryWrapper = new LambdaQueryWrapper<>();
//		queryWrapper.eq(CompetitionDetail::getId,competitionDetail.getId());
		boolean update = competitionDetailService.updateById(competitionDetail);
		if (update){
			return	ActionResultService.callBackResultOK("更新成功");
		}
		return ActionResultService.callBackResultFail("更新失败");
	}
}
